<template>
  <div>
    <el-button
      type="primary"
      @click="
        model = {};
        getdata();
      "
      >添加分类</el-button
    >
    <Table :lists="list" :column="col" rowKey="cat_id">
      <template v-slot:deleted="{ row }">
        <el-icon color="red" v-if="row.cat_deleted == true">
          <CircleClose />
        </el-icon>
        <el-icon color="green" v-if="row.cat_deleted == false">
          <CircleCheck />
        </el-icon>
      </template>
      <template v-slot:level="{ row: { cat_level } }">
        <el-tag :type="colors[cat_level]">{{ levels[cat_level] }}</el-tag>
      </template>
      <template v-slot:action="{ row }">
        <el-button
          size="small"
          type="primary"
          @click="
            model = { ...row };
            dialogFormVisible = true;
          "
          >编辑</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="getdelCategories(row.cat_id)"
          >删除</el-button
        >
      </template>
    </Table>

    <!-- 分页 -->
    <el-pagination
      @size-change="
        (v) => {
          queryModel.pagesize = v;
          getList();
        }
      "
      @current-change="
        (v) => {
          queryModel.pagenum = v;
          getList();
        }
      "
      :current-page="queryModel.pagenum"
      :page-sizes="[5, 10, 15, 20, 25, 30, 35, 40]"
      :page-size="queryModel.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
      <!-- :page-sizes="[1, 2, 3, 4]"-->
    </el-pagination>
    <!-- 弹出框 -->
    <el-dialog
      v-model="dialogFormVisible"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-form :model="model" :rules="rules" label-width="120px" status-icon>
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="model.cat_name" />
        </el-form-item>
        <el-form-item label="父级分类: " prop="cat_pid" v-if="!model.cat_id">
          <!-- 联级选择器 -->
          <el-cascader
            :options="list"
            v-model="model.cat_pid"
            :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
            }"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="getAddCategories">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/http/goods.js";
import { reactive, onMounted, toRefs, computed } from "vue";
import { ElMessageBox } from "element-plus";
export default {
  name: "categories",
  setup() {
    const state = reactive({
      levels: ["一级", "二级", "三级"],
      colors: ["", "success", "warning"],
      /**
       * 搜素数据
       */
      queryModel: {
        type: 3,
        pagenum: 1,
        pagesize: 10,
      },
      /**
       * 总条数
       */
      total: 0,
      /**
       * 数据模型
       */
      model: {},
      /**
       * 规则
       */
      rules: {
        cat_name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
        ],
      },
      /**
       * 弹出框的显示
       */
      dialogFormVisible: false,
      /**
       * 存储数据
       */
      list: [],
      /**
       * 表格配置项
       */
      col: [
        {
          type: "index",
          label: "序号",
          width: "100",
        },
        {
          prop: "cat_name",
          label: "分类名称",
        },
        {
          slot: "deleted",
          label: "是否有效",
        },
        {
          slot: "level",
          label: "分类排序",
        },
        {
          label: "操作",
          slot: "action",
        },
      ],
    });
    let getdata = async () => {
      state.dialogFormVisible = true;
      const res = await api.CategoriesList({ type: 2 });
      state.list = res.data;
      console.log(res, "2");
    };
    /**
     * 删除用户
     */
    let getdelCategories = (id) => {
      ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
          const res = await api.delCategories(id);
          // console.log(res, "row");
          if (res.meta.status == 200) {
            getList();
          }
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消删除",
          });
        });
    };
    /**
     * 添加编辑
     */
    let getAddCategories = async () => {
      console.log(state.model, "state.model");
      state.model.cat_pid =
        state.model.cat_pid.length > 0 ? state.model.cat_pid : [];
      const obj = {
        ...state.model,
        cat_pid: [...state.model.cat_pid].slice(-1)[0] || 0,
        cat_level: state.model.cat_pid.length || 0,
      };
      const res = await (state.model.cat_id
        ? api.UpdataCategories
        : api.AddCategories)(obj);

      if (res.meta.status == 200 || res.meta.status == 201) {
        state.dialogFormVisible = false;
        getList();
      }
    };
    /**
     * 获取数据
     */
    let getList = async () => {
      const res = await api.CategoriesList(state.queryModel);
      console.log(res.data);
      if (res.meta.status == 200) {
        state.list = res.data.result;
        state.total = res.data.total;
        console.log(state.list, "权限");
      }
    };
    /**
     * 标题
     */
    let title = computed(() => {
      return state.model.cat_id ? "编辑分类 " : "添加分类";
    });
    onMounted(() => {
      getList();
    });

    return {
      ...toRefs(state),
      getList, //获取数据
      getdata,
      title, //标题
      getAddCategories, //添加 编辑 角色
      getdelCategories, //删除角色
    };
  },
};
</script>

<style lang="scss" scoped>
</style>